package com.lenovo.lmrp.controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.ws.rs.QueryParam;

import org.apache.taglibs.bsf.expression;
//import org.jboss.resteasy.client.jaxrs.i18n.LogMessages_.logger;
//import org.apache.poi.hssf.model.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.type.TypeReference;
import com.lenovo.lmrp.api.itsmgateway.IItsmProject;
import com.lenovo.lmrp.api.model.RestResponse;
import com.lenovo.lmrp.api.model.entity.ProjectExtensePO;
import com.lenovo.lmrp.api.model.entity.ProjectPO;
import com.lenovo.lmrp.util.RestClientFactory;
import com.lenovo.sdc.framework.common.base.BaseRestSpringController;
import com.lenovo.sdc.framework.rest.RestClientProxy;
import com.lenovo.sdc.framework.util.AttributeBean;
import com.lenovo.sdc.framework.util.JSONHelper;
import com.lenovo.sdc.framework.util.PageData;

/**
 * 
 * 
 * @author feipeng
 * @version $Id: ProjectController.java, v 0.1 2016年4月14日 下午8:53:31 feipeng Exp $
 */
@Controller
public class ProjectController extends BaseRestSpringController {
		
	@Autowired
    RestClientFactory restClientFactory = new RestClientFactory();

    /**
     * 请求跳转项目index页面
     * @author zx-lxj
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/projectIndex")
    public String index() throws Exception {
        return "/project/show";
    }
	
	/**
	 * 根据项目id, 名称，状态查询状态
	 * 
	 * @param id
	 * @param name
	 * @param status
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/projectSearch")
	public ModelAndView search(@RequestParam("id") String id,
	                           @RequestParam("name") String name,
	                           @RequestParam("status") String status
                               ) throws Exception{
	    final int MIN_VALUE = 2147483647;
	    ModelAndView mav=new ModelAndView();
	    try{
	        RestClientProxy lmrpItsmProxy=restClientFactory.getItsmGatewayClient();
	        if(Objects.nonNull(lmrpItsmProxy)){
	            IItsmProject iItsmProject =lmrpItsmProxy.getService(IItsmProject.class);
	                if(Objects.nonNull(iItsmProject)){
	                    RestResponse<PageData<ProjectPO>> listProject = iItsmProject.listProject("xihe", "%"+id+"%", "%"+name+"%", status, 0, MIN_VALUE);
	                    if(Objects.nonNull(listProject)){
	                        List<ProjectPO> projectList = listProject.getResult().getPageData();
	                        mav.addObject("projectlist", projectList);
	                        mav.addObject("id", id);
	                        mav.addObject("name", name);
	                        mav.addObject("status", status);
	                        mav.setViewName("/project/show");
	                    }else{
	                        log.error("error query projectlist is null");
	                    }
	                }else{
	                    log.error("error no find IItsmProject.class");
	                }
	        }else{
	            log.error("error ItsmGateWayProxy interface exception");
	        }
	    }catch(Exception e){
	        log.error(e.getMessage(),e);
	    }    
        return mav;
	}

	/**
	 * 根据项目id进入相应的编辑页面
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/projectEdit")
    public ModelAndView edit( @RequestParam("id") String id
                             )throws Exception{
	    final int MIN_VALUE = 2147483647;
	    ModelAndView mav=new ModelAndView();
	    try{
	        RestClientProxy lmrpItsmProxy=restClientFactory.getItsmGatewayClient();
	        if(Objects.nonNull(lmrpItsmProxy)){
	            IItsmProject iItsmProject =lmrpItsmProxy.getService(IItsmProject.class);
	            if(Objects.nonNull(iItsmProject)){
	                RestResponse<PageData<ProjectPO>> listProject = iItsmProject.listProject("xihe", id, null, null, 0, MIN_VALUE); 
	                    if(Objects.nonNull(listProject)){
	                        List<ProjectPO> projectList = listProject.getResult().getPageData();
	                        if(projectList!=null&&projectList.size()>0){
	                            mav.addObject("project", projectList.get(0));
	                        }else{
	                            log.error("project is null or empty");
	                        }
	                    }else{
	                        log.error("error query projectlist is null");
	                    }    
		                RestResponse<ProjectPO> project = iItsmProject.getProject("xihe", id);
    	                if(Objects.nonNull(project)){ 
                             Object projectGroupInfo = project.getResult().getOthers().getAttributeBean().get("rolelist");
                             Object projectGroupTickettype=project.getResult().getOthers().getAttributeBean().get("tickettype");
                             mav.addObject("projectGroupInfo", JSONHelper.toJSON(projectGroupInfo));
                             mav.addObject("tickettype",JSONHelper.toJSON(projectGroupTickettype));
                             mav.addObject("projectid",id);
                             log.info("+++++++"+JSONHelper.toJSON(projectGroupTickettype)+"++++++++++++++++++");
                             log.info("============="+JSONHelper.toJSON(projectGroupInfo));
                          }else{
                              log.error("error query project is null");
                          }
	            }else{
	                log.error("error no find IItsmProject.class");
	            }
	        }else{
	            log.error("error ItsmGateWayProxy interface exception");
	        }
	    }catch(Exception e){
	        log.error(e.getMessage(),e);
	    }
	    mav.setViewName("/project/edit");
        return mav;
    }
	
	/**
	 * 更新项目组信息
	 * 
	 * @param projectid
	 * @param role
	 * @param roleDescription
	 * @param teleNum
	 * @return
	 * @throws Exception
	 */
    @RequestMapping(value="projectGroupUpdate")
    @ResponseBody
    public int projectGroupSave(@RequestParam("itsmcode") String itsmcode, 
                                   @RequestParam("json") String json,
                                   @RequestParam("id") String id) {
    log.info("+++++++++++++进入项目组更新函数+++++++++++++++");
    log.info("++++++++++++"+json+"++++++++++++++");
    log.info("++++++++++++"+id+"++++++++++++++");
    log.info("++++++++++++"+itsmcode+"++++++++++++++");
    String jsonLastword=json.substring(json.length()-1, json.length());
    if(jsonLastword.equals(",")){
        json=json.substring(0, json.length()-1)+"]}";
    }
    ModelAndView mav=new ModelAndView();
    ProjectExtensePO projectExtensePO = new ProjectExtensePO();
    projectExtensePO.setId(id);
    projectExtensePO.setExtprops(json);
    RestResponse<ProjectExtensePO> projectExtProp = new  RestResponse<ProjectExtensePO>();
    try{ 
        RestClientProxy lmrpItsmProxy=restClientFactory.getItsmGatewayClient();
        IItsmProject iItsmProject = lmrpItsmProxy.getService(IItsmProject.class);
        projectExtProp =iItsmProject.updateProjectExtProps(itsmcode, projectExtensePO);  
        
    }catch(Exception e){
        log.error(e.getMessage(),e);
    } 
    return projectExtProp.getEcode();
    }
}
